import java.util.Arrays;
import java.util.Comparator;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2022-08-19
 * Time: 18:23
 */
class Person implements Comparable <Person>{
    private String name;
    private int age;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Person o) {
        if(this.age-o.age>0){
            return 1;
        } else if (this.age-o.age<0) {
            return -1;
        }else{
            return 0;
        }
    }
}

class NameComparator implements Comparator<Person> {

    @Override
    public int compare(Person o1, Person o2) {
        return o1.getName().compareTo(o2.getName());
    }
}

public class Test2_0819 {

    public static void main(String[] args) {
        Person[] person1 = new Person[]{
                new Person("张三", 97),
                new Person(";李四", 98),
                new Person("王五", 95),
                new Person("赵六", 93)};

        Person[] person2 = new Person[]{
                new Person("zhangsan", 97),
                new Person(";lisi", 98),
                new Person("wangwu", 95),
                new Person("zhaoliu", 93)};

        Arrays.sort(person1); //按照年龄升序
        System.out.println(Arrays.toString(person1));

        System.out.println("================================");
        //修改为按照姓名升序
        //利用比较器comparator 重写compare方法

        NameComparator nameComparator = new NameComparator();
        //Comparator比较器使用时需要new一个对应得比较器
        //Arrays。sort在使用时，可以传入一个比较器进行排序
        Arrays.sort(person1,nameComparator);

        System.out.println(Arrays.toString(person1));

        System.out.println("=================");
        Arrays.sort(person2,nameComparator);
        System.out.println(Arrays.toString(person2));

    }
}
